<?php
namespace app\common\controller;
use think\Request;
use think\Db;
use app\common\controller\Init;

class AdminBase extends Init
{
    public function _initialize()
    {
        parent::_initialize();
        if (!Request::instance()->isPost()) {
            header('Content-type: application/json');  
            exit(json_encode(array('code' => -1 ,'msg' => '非法请求'),true));
        }
		
        $header = Request::instance()->header();
        $passStatus = false;
        if (!$this->adminId) {
        	$time = $header['request-time'];
        	if (!$this->keyInfo['token']) {
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode(['code'=>-1, 'msg'=>'请配置接口参数']));
        	}
            if (empty($header['request-token']) || empty($time)) {
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode(['code'=>-1, 'msg'=>'缺少请求的必要参数']));
            }
            if (((time() - 3600) > $time) || ((time() + 3600) < $time)) {
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode(['code'=>-1, 'msg'=>'请求超时']));
            }
            if ($header['request-token'] != md5($time . md5($this->keyInfo['token']))) {
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode(['code'=>-1, 'msg'=>'TOKEN错误']));
            }
          	$passStatus = true;
        } else {
         	$passStatus = true;
        }
        if (!$passStatus) {
            header('Content-Type:application/json; charset=utf-8');
            exit(json_encode(['code'=>-1, 'msg'=>'无权限操作']));
        }
    }
    
}
